﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Time.Model;

namespace Time.App
{
    public partial class ManageProjectWindow : Form
    {
        private TimeDataContext db = new TimeDataContext();

        public ManageProjectWindow(Form owner)
        {
            this.Owner = owner;
            InitializeComponent();

            List<Project> projects = db.Projects.Where(p => p.LicenseId == 1 && p.Parent == null).ToList();

            foreach (Project project in projects)
            {
                TreeNode root = treeView1.Nodes.Add(project.Name);
                root.Tag = project;
                PopulateTreeView(root, project);
            }
        }

        private void PopulateTreeView(TreeNode rootNode, Project project)
        {
            foreach (Project subproject in project.Projects)
            {
                TreeNode current = rootNode.Nodes.Add(subproject.Name);

                if (subproject.Projects.Count > 0)
                    PopulateTreeView(current, subproject);
            }
        }
    }
}
